package com.cenntro.common.model.dbo;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.cenntro.common.util.NumUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class BaseDO implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6593919029831484341L;
	private String createUser;
	private String updateUser;
	private Date cdt;
	private Date udt;

	
	@JsonIgnore
	private Integer limit = 10;
	@JsonIgnore
	private Integer offset;
	@JsonIgnore
	private Integer page = 1;
	@JsonIgnore
	private String sort = "desc";
	
	
	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public String getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}

	public Date getCdt() {
		return cdt;
	}

	public void setCdt(Date cdt) {
		this.cdt = cdt;
	}

	public Date getUdt() {
		return udt;
	}

	public void setUdt(Date udt) {
		this.udt = udt;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
		this.offset = (NumUtil.toInt(page)-1)* this.limit;
		this.offset = this.offset < 0 ? 0 : this.offset;
	}

	public Integer getOffset() {
		return offset;
	}

	public void setOffset(Integer offset) {
		this.offset = offset;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.offset = (NumUtil.toInt(page)-1)* this.limit;
		this.offset = this.offset < 0 ? 0 : this.offset;
		this.page = page;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
	}
	
    public void validateParams(Class... groupClass) {
        //调用JSR303验证工具，校验参数
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<Object>> violations = null;
        if(groupClass == null){
            violations = validator.validate(this);
        }else {
            violations = validator.validate(this, groupClass);
        }
        
        Iterator<ConstraintViolation<Object>> iter = violations.iterator();
        if (iter.hasNext()) {
            String errMessage = iter.next().getMessage();
            log.info("参数不符", errMessage);
//            throw new CustomException(errMessage);
        }
    }
    
    @Override
    public boolean equals(Object obj) {
    	
    	return super.equals(obj);
    }
}
